package com.zuul.gateway.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * create by yongli on 2021-10-08 17:21
 * Interceptor需要注册到注册器中，才能生效
 */

public class GuardInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(GuardInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("ZUUL_TOKEN");
        if (token.equals("zuul")) {
            logger.info("密令没错，是来自网关的请求");
        } else {
            logger.info("不是来自网关的请求，但是目前也让通过");
            // response.getWriter().write("直接返回");
        }
        return true;
    }
}
